/**
 * Copyright &copy; 2012-2015  All rights reserved.
 */
package com.smartwork.modules.sys.web;

import com.smartwork.common.config.Global;
import com.smartwork.common.persistence.Page;
import com.smartwork.common.utils.StringUtils;
import com.smartwork.common.web.BaseController;
import com.smartwork.modules.sys.entity.ClientVersion;
import com.smartwork.modules.sys.service.ClientVersionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 客户端版本管理Controller
 * @author Albertyan
 * @version 2015-12-25
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/clientVersion")
public class ClientVersionController extends BaseController {

	@Autowired
	private ClientVersionService clientVersionService;
	
	@ModelAttribute
	public ClientVersion get(@RequestParam(required=false) String id) {
		ClientVersion entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = clientVersionService.get(id);
		}
		if (entity == null){
			entity = new ClientVersion();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:clientVersion:view")
	@RequestMapping(value = {"list", ""})
	public String list(ClientVersion clientVersion, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ClientVersion> page = clientVersionService.findPage(new Page<ClientVersion>(request, response), clientVersion); 
		model.addAttribute("page", page);
		return "modules/sys/clientVersionList";
	}

	@RequiresPermissions("sys:clientVersion:view")
	@RequestMapping(value = "form")
	public String form(ClientVersion clientVersion, Model model) {
		model.addAttribute("clientVersion", clientVersion);
		return "modules/sys/clientVersionForm";
	}

	@RequiresPermissions("sys:clientVersion:edit")
	@RequestMapping(value = "save")
	public String save(ClientVersion clientVersion, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, clientVersion)){
			return form(clientVersion, model);
		}
		clientVersionService.save(clientVersion);
		addMessage(redirectAttributes, "保存客户端版本管理成功");
		return "redirect:"+Global.getAdminPath()+"/sys/clientVersion/?repage";
	}
	
	@RequiresPermissions("sys:clientVersion:edit")
	@RequestMapping(value = "delete")
	public String delete(ClientVersion clientVersion, RedirectAttributes redirectAttributes) {
		clientVersionService.delete(clientVersion);
		addMessage(redirectAttributes, "删除客户端版本管理成功");
		return "redirect:"+Global.getAdminPath()+"/sys/clientVersion/?repage";
	}




}